const cookie = new Cookie();
const token = cookie.getCookie('token');
//初始化页面函数
$(function () {
  //获取用户登录状态
  getUserStatu();
  //请求商品详情
  getFavorList()
})
//获取用户登录状态
function getUserStatu() {
  if (!cookie.getCookie('uid')) {
    hsycms.tips('当前未登录，即将跳转到首页登陆', () => {
      setTimeout(() => {
        location.href = './index.html';
      }, 1500)
    })
    return
  } else {
    $('.username').text(cookie.getCookie('username'));
    $('.mycart').text(cookie.getCookie('username') + '的购物车');
  }
}

//进入购物车
$('.mycart').click(function () {
  console.log(111);
  if (!cookie.getCookie('uid')) {
    //如果用户未登录，点击按钮登录
    hsycms.confirm('您未登录，是否跳转到登录页登录？',
      //前往登录页
      function () {
        hsycms.loading('即将前往登录页');
        setTimeout(function () {
          location.href = './login.html'
          hsycms.closeAll();
        }, 1500)
      },
      //点击取消
      function () { },
    )
  } else {
    //如果用户登录了，进入购物车页面
    location.href = './cart.html';
  }
})

//请求收藏数据
function getFavorList() {
  let { id } = getQueryString();
  request({
    type: 'post',
    url: 'https://api.it120.cc/conner/shop/goods/detail',
    data: {
      id
    }
  }).then(res => {
    const { basicInfo, category, content, pics2 } = res.data
    const pic = pics2[0]
    let str = '';
    str += `
            <div class="media">
              <div class="media-left">
                  <img class="media-object" src="${pic}" alt="">
              </div>
              <div class="media-body">
                <h2 class="media-heading good-name">${basicInfo.name}</h2>
                <h3 class="media-heading good-content">${content}</h3>
                <p class="media-heading"><span class="category">所属分类:</span><span class="category-name">${category.name}</span></p>
              
                <p class="media-heading"><a href="javascript:;" class="btn btn-success btn-md add-cart" data-id="${basicInfo.id}">加入购物车</a>
                    <a href="javascript:;" class="btn btn-warning btn-md add-favor" data-id="${basicInfo.id}" data-type="${basicInfo.type}">加入收藏</a>
                </p>
              </div>
            </div>
            `
    $('.section').append(str)
  })
}

//点击加入购物车
$('.section').on('click', '.add-cart', function () {
  //拿到当前点击对象的id
  let goodsId = $(this).attr('data-id');
  let token = cookie.getCookie('token')
  addCart(goodsId, 1, token)
})
//加入购物车
function addCart(goodsId, number, token) {
  if (!token) {
    //如果用户未登录，点击按钮登录
    hsycms.confirm('您未登录，是否跳转到登录页登录？',
      //前往登录页
      function () {
        hsycms.loading('即将前往登录页');
        setTimeout(function () {
          location.href = './login.html'
          hsycms.closeAll();
        }, 1500)
      },
      //点击取消
      function () {
        // hsycms.fail('点击了取消');
      },
    )
    return
  }
  request({
    type: 'post',
    url: 'https://api.it120.cc/conner/shopping-cart/add',
    data: {
      goodsId,
      number,
      token
    },
    loading: true,
    timeout: 3000
  }).then(res => {
    hsycms.alert('加入购物车成功', () => {
    }, 1500)
  })
}

//点击加入收藏
$('.section').on('click', '.add-favor', function () {
  let goodsId = $(this).attr('data-id');
  let type = $(this).attr('data-type');
  request({
    type: 'post',
    url: 'https://api.it120.cc/conner/shop/goods/fav/add',
    data: {
      goodsId,
      type,
      token
    },
  }).then(res => {
    hsycms.success('加入收藏成功')
  })
})